/*
 * The Open Source Unix Installer
 *
 * The MIT License
 *
 * Copyright 2011-2012 Andrey Pudov.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/*
 * @author  Andrey Pudov	<andrey@andreypudov.com>
 * @version 0.00.00
 * @name    window.c
 * @date:   Dec 28, 2011
 */

#include "window.h"

Window* installer_window_new(int argc, char **argv) {
	gtk_init(&argc, &argv);

	/* create a new window */
	Window *window = (Window*)malloc(sizeof(Window));
	if (window == NULL) {
	    //TODO Add Logger
	    exit(EXIT_FAILURE);
	}
	window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

	window->vertical_box   = gtk_vbox_new(FALSE, 0);
	window->horizontal_box = gtk_hbox_new(FALSE, 0);

	window->product_label = gtk_label_new(NULL);

	window->separator = gtk_hseparator_new();

	window->control_panel = installer_control_panel_new();

    gtk_window_set_title(GTK_WINDOW(window->window), "The Open Source Unix Installer");
    gtk_window_set_default_size(GTK_WINDOW(window->window), 800, 600);
//  gtk_window_set_resizable(GTK_WINDOW(window->window), FALSE);
    gtk_window_set_position(GTK_WINDOW(window->window), GTK_WIN_POS_CENTER);

    window->markup = g_markup_printf_escaped("<span foreground=\"blue\" size=\"x-large\">%s</span>",
			"Intel(R) Threading Building Blocks\nUpdate 1 for Windows*");
	gtk_label_set_markup(GTK_LABEL(window->product_label), window->markup);
	g_free(window->markup);

	//gtk_box_pack_start(GTK_BOX(window->vertical_box), controlPanel->getContainer(), FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(window->vertical_box), window->product_label, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(window->vertical_box), window->separator, FALSE, FALSE, 0);
	gtk_box_pack_end(GTK_BOX(window->vertical_box),
	        installer_control_panel_get_container(window->control_panel), FALSE, FALSE, 0);

	gtk_container_add(GTK_CONTAINER(window->window), window->vertical_box);

	installer_control_panel_back_button_set_enabled(window->control_panel, false);

	gtk_widget_show_all(window->window);

	/*
	 * Connects the delete_event signal to the main window. This is very important
	 * for proper intuitive behavior.
	 */
	g_signal_connect(window->window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);

	/*
	 * Connects the "destroy" event to a signal handler.
	 * This event occurs when we call gtk_widget_destroy() on the window,
	 * or if we return FALSE in the "delete-event" callback.
	 */
	g_signal_connect(window->window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

	/*
	 * All GTK applications must have a gtk_main(). Control ends here
	 * and waits for an event to occur (like a key press or
	 * mouse event).
	 */
	gtk_main();

	return window;
}

void installer_window_delete(Window *window) {
    installer_control_panel_delete(window->control_panel);

	free(window);
}
